import {Global} from '../../mongodb';
import {ApiCall} from 'tsrpc';
import {ReqAdd, ResAdd} from '../../shared/protocols/users/PtlAdd';
import * as crypto from 'crypto';

export default async function (call: ApiCall<ReqAdd, ResAdd>) {
    // 检查密码是否存在，如果为空字符串，则设置默认密码
    if (call.req.query.password === '') {
        call.req.query.password = '123456';
    }

    // 创建一个 SHA-256 hash
    // 获取加密后的密码
    // 将加密后的密码赋值回 query 对象
    call.req.query.password = crypto.createHash('sha256').update(call.req.query.password).digest('hex');
    const ret = await Global.db.collection('users').insertOne(call.req.query);
    return call.succ({newID: ret.insertedId.toString()});

}